package com.zuihai.app.mvp.ui.widget.refresh;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.widget.GifView;

/**
 * Created by Administrator on 2017/7/5 0005.
 */

public class RefreshHeader extends FrameLayout implements JRefreshHeader {

    private GifView mGifView;
    private TextView mHintView;
    private View mRootView;

    public RefreshHeader(@NonNull Context context) {
        this(context, null);
    }

    public RefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRootView = LayoutInflater.from(context).inflate(R.layout.layout_refresh, null, false);
        mGifView = (GifView) mRootView.findViewById(R.id.gif_view);
        mHintView = (TextView) mRootView.findViewById(R.id.tv_hint);
        addView(mRootView);
    }

    @Override
    public long succeedRetention() {
        return 200;
    }

    @Override
    public long failingRetention() {
        return 200;
    }

    @Override
    public int refreshHeight() {
        return mRootView.getHeight();
    }

    @Override
    public int maxOffsetHeight() {
        return mRootView.getHeight() * 4;
    }

    @Override
    public void onReset(JRefreshLayout refreshLayout) {
        mGifView.pause();
    }

    @Override
    public void onPrepare(JRefreshLayout refreshLayout) {
    }

    @Override
    public void onRefresh(JRefreshLayout refreshLayout) {
        if (mGifView != null && !mGifView.isPlaying()){
            mGifView.play();
        }
    }

    @Override
    public void onComplete(JRefreshLayout refreshLayout, boolean isSuccess) {
        if (mGifView.isPlaying()) {
            mGifView.pause();
        }
    }

    @Override
    public void onScroll(JRefreshLayout refreshLayout, int distance, float percent, boolean refreshing) {
    }
}
